package com.example.demo3.controller;


import com.dsbj.common.msg.DataPageMessage;
import com.dsbj.common.msg.ResponseMessage;
import com.example.demo3.entity.DictData;
import com.example.demo3.entity.SpareParts;
import com.example.demo3.service.DictTypeService;
import com.example.demo3.service.SparePartsService;
import com.example.demo3.utils.constant.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 备件档案 前端控制器
 * </p>
 *
 * @author xsyuan
 * @since 2020-03-26
 */
@Controller
@RequestMapping("spareParts")
public class SparePartsController extends BaseController {

    @Resource
    private DictTypeService dictTypeService;

    @Resource
    private SparePartsService sparePartsService;

    @RequestMapping("list")
    public void list(){}

    @RequestMapping("sparePartsPageInfo")
    @ResponseBody
    public DataPageMessage sparePartsPageInfo(int pageindex, String orderdir, int pagesize, String ordercolumn, int sEcho,String textVague,String codeVague,String nameVague){
/*        Page<SpareParts> sparePartsPage =sparePartsService.getSelectPage(pageindex,pagesize);
        if(sparePartsPage==null||sparePartsPage.getRecords()==null){
            return DataPageMessage.ok(null,(int)sparePartsPage.getCurrent(),(int)sparePartsPage.getCurrent(),sEcho,"");
        }
        return DataPageMessage.ok(sparePartsPage.getRecords(),(int)sparePartsPage.getTotal(),(int)sparePartsPage.getTotal(),sEcho,"");*/
        Map<Object,Object> map = new HashMap<>();
            map.put("pageindex",pageindex);
            map.put("pagesize",pagesize);
            map.put("orderdir",orderdir);
            map.put("ordercolumn",ordercolumn);
            if(StringUtils.isNotBlank(textVague)){
                map.put("textVague",textVague);
            }
            if(StringUtils.isNotBlank(codeVague)){
                map.put("codeVague",codeVague);
            }
            if(StringUtils.isNotBlank(nameVague)){
                map.put("nameVague",nameVague);
            }
        int countPage=sparePartsService.countPage(map);
        List<SpareParts> resourceDeviceList=sparePartsService.queryPage(map);
        return DataPageMessage.ok(resourceDeviceList,countPage,countPage,sEcho,"");
    }

    @RequestMapping("add")
    public String add (Model model){
        List<DictData> dictDataList =dictTypeService.getByType(Constants.DICT_TYPE_SPAREPARTS_TYPE);
        model.addAttribute("dictDataList",dictDataList);
        return "spareParts/add";
    }

    @RequestMapping("edit")
    public String edit (Model model, String id){
        if(StringUtils.isNotBlank(id)){
            Map<String,Object> map  = new HashMap<>();
                map.put("id",id);
                map.put("delFlag",0);
            List<SpareParts> sparePartsList = sparePartsService.getByMap(map);
            if(sparePartsList.size() >0 && sparePartsList != null && !sparePartsList.isEmpty()){
                model.addAttribute("spareParts",sparePartsList.get(0));
            }
        }
        return "spareParts/edit";
    }

    @RequestMapping("del")
    @ResponseBody
    public ResponseMessage del (SpareParts spareParts){
        //sparePartsService.delete(id);
        spareParts.setDelFlag(1);
        sparePartsService.update(spareParts);
        return ResponseMessage.ok("删除成功");
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public ResponseMessage saveOrUpdate(SpareParts spareParts){
        spareParts.setCreateUser("xsyuan");
        spareParts.setCreateTime(new Date());
        String msg= "";
        if(StringUtils.isNotBlank(spareParts.getId())){
            spareParts.setUpdateUser("xsyuan");
            spareParts.setUpdateTime(new Date());
            sparePartsService.update(spareParts);
            msg = "修改成功";
        }else{
            spareParts.setCreateUser("xsyuan");
            spareParts.setCreateTime(new Date());
            sparePartsService.insert(spareParts);
            msg = "添加成功";
        }
        return ResponseMessage.ok(msg);
    }

}
